<?php
namespace app\admin\controller;

use app\common\model\Com;
use app\common\model\Dig;
use app\common\model\District;
use app\common\model\Follow;
use app\common\model\Grade;
use app\common\model\Platform;
use app\common\model\User as UserModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
use think\Request;

/**
 * 用户管理
 * Class AdminUser
 * @package app\admin\controller
 */
class User extends AdminBase
{
    protected $user_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->user_model = new UserModel();
    }




    /**
     * 用户管理
     * @param string $keyword
     * @param int $page
     * @return mixed
     */
    public function index(Request $request)
    {
        $keyword      = $request->param('keyword') ?: '';
        $page         = $request->param('page') ?: 1;
        $user_id      = $request->param('user_id');
        $user_account = $request->param('user_account');
        $sex_select   = $request->param('sex_select') ?: '';
        $platform     = $request->param('platform');
        $start_time   = $request->param('start_time') ?: '';
        $end_time     = $request->param('end_time') ?: '';

        $map = [];
        if ($keyword) {
            $map['username|mobile|email|id'] = ['like', "%{$keyword}%"];
        }
        if ($user_id) {
            $map['id'] = $user_id;
        }
        if ($user_account) {
            $map['open_id|mobile'] = ['like', "%{$user_account}%"];
        }
        if ($sex_select) {
            $map['sex'] = $sex_select;
        }
        if ($start_time && $end_time) {
            $time1              = ($start_time);
            $time2              = ($end_time);
            $map['create_time'] = ['between', "$time1,$time2"];
        }
        $user_list = UserModel::where($map)->field([
            'id',
            'create_time',
            'mobile',
            'open_id',
            'username',
            'coin',
            'balance',
            'sex',
            'address',
            'integral',
            'status',
            'platform',
            'ID_name',
        ])->order('id DESC')->paginate(15, false, [
            'page' => $page,
            'query' => [
                'keyword' => $keyword,
                'sex_select' => $sex_select,
                'start_time' => $start_time,
                'end_time' => $end_time,
                'platform' => $platform,
            ]
        ]);

        foreach ($user_list as $item => $value) {
            $user_list[$item]['gradename']     = getGradeName($value['integral']);
            $user_list[$item]['ID_name']       = $value['ID_name'] ?: '暂无';
            $user_list[$item]['address']       = $value['address'] ?: '暂无';
            $user_list[$item]['mobile']        = $value['mobile'] ?: '暂无';
            $user_list[$item]['username']      = $value['username'] ?: '暂无';
            $user_list[$item]['sex_name']      = $value['sex'] == 1 ? '男' : (($value['sex'] == 2) ? '女' : '未知');
            $user_list[$item]['account_name']  = empty($value['mobile']) ? (empty($value['open_id']) ? '暂无' : $value['open_id']) : $value['mobile'];
        }
        $sex_arr      = [
            1 => '男',
            2 => '女'
        ];
        $all_user_num = UserModel::where($map)->count('id');

        return $this->fetch('index', [
            'user_list' => $user_list,
            'keyword' => $keyword,
            'sex_select' => $sex_select,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'sex_arr' => $sex_arr,
            'all_user_num' => $all_user_num,
        ]);
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存用户
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'User');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['password'] = md5($data['password'] . Config::get('salt'));
                if ($this->user_model->allowField(true)->save($data)) {
                    $action = '用户' . $_REQUEST['username'];
                    $this->getLog("1", $action);
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑用户
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $user = UserModel::where('id', $id)
            ->field([
                'mobile',
                'open_id',
                'username',
                'sex',
                'id',
                'age',
                'integral',
                'create_time',
                'platform',
                'grade_level',
                'status',
                'photo',
                'address',
                'coin',
                'balance',
                'integral'
            ])
            ->find();


        return $this->fetch('edit', ['user' => $user]);
    }

    /**
     * 更新用户
     * @param $id
     */
    public function update(Request $request)
    {
        $id                  = $request->param('id');
        $data['mobile']      = $request->param('mobile');
        $data['username']    = $request->param('username');
        $data['grade_level'] = $request->param('grade_level');
        $data['platform']    = $request->param('platform');
        $data['password']    = encryPassword($request->param('password'));
        $data['integral']    = getGradeByLevel($data['grade_level']);

        if (UserModel::where('id', $id)->update($data)) {
            $action = '用户ID' . $id;
            $this->getLog("3", $action);
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }

    }

    /**
     * 删除用户
     * @param $id
     */
    public function delete($id)
    {
        if ($this->user_model->destroy($id)) {
            $action = '删除用户ID' . $id;
            $this->getLog("2", $action);
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


    public function changeUserStatus(Request $request)
    {
        dd($request);
    }
}